﻿using DG.Tweening;
using System.Collections.Generic;
using UnityEngine;
using ZGameFramework;


public class FlyManager : MonoBehaviour
{
    static FlyManager _instance = null;
    public static FlyManager Instance
    {
        get
        {
            if ( null == _instance) {
                GameObject obj = new GameObject();
                obj.name = "[FlyManager]";
                DontDestroyOnLoad( obj );
                _instance = obj.AddComponent<FlyManager>();
            }
            return _instance;
        }
    }

    FlyManager() { }


    //IDAllocer_Int64 m_IDAllocer = new IDAllocer_Int64(1);
    //StackObjPool s_pool = new StackObjPool(100);
    long m_IdAllocer = 1;
    Dictionary<long, FlyPlayer> m_dicQuery = new Dictionary<long, FlyPlayer>();
    List<FlyPlayer> m_updateList = new List<FlyPlayer>();
     
    public FlyPlayer PlayFly(Transform _transform, Vector3 p1, Vector3 p2, float duration, float disRatio, float heightRatio, float angle = 0)
    {
        FlyPlayer player = ReferencePool.Acquire<FlyPlayer>();
        if (player == null)
            player = new FlyPlayer();
        player.Init(m_IdAllocer++, _transform, p1, p2, duration, disRatio, heightRatio, angle);
        m_dicQuery.Add(player.LogicID, player);
        m_updateList.Add(player);
        return player;
    }

    public FlyPlayer PlayFly(Transform _transform, Vector3 p1, Vector3 p2, float duration, Vector2 _center)
    {
        FlyPlayer player = ReferencePool.Acquire<FlyPlayer>();
        if (player == null)
            player = new FlyPlayer();
        player.Init(m_IdAllocer++, _transform, p1, p2, duration, _center );
        m_dicQuery.Add(player.LogicID, player);
        m_updateList.Add(player);
        return player;
    }


    public FlyPlayer PlayFly(Transform _transform, Vector3 p1, Vector3 p2, float duration, Ease ease = Ease.Linear)
    {
        FlyPlayer player = ReferencePool.Acquire<FlyPlayer>();
        if (player == null)
            player = new FlyPlayer();
        player.Init(m_IdAllocer++, _transform, p1, p2, duration, ease);
        m_dicQuery.Add(player.LogicID, player);
        m_updateList.Add(player);
        return player;
    }

    public FlyPlayer PlayFly(Transform _transform, Vector3 v, float duration)
    {
        FlyPlayer player = ReferencePool.Acquire<FlyPlayer>();
        if (player == null)
            player = new FlyPlayer();
        player.Init(m_IdAllocer++, _transform, v, duration);
        m_dicQuery.Add(player.LogicID, player);
        m_updateList.Add(player);
        return player;
    }

    public void StopFly( long id )
    {
    }

    public void OnLateUpdate(float deltaTime)
    {
        for (int i = m_updateList.Count - 1; i >= 0; --i)
        {
            FlyPlayer p = m_updateList[i];
            p.OnLateUpdate(deltaTime);
            if (!p.m_isActive)
            {
                m_updateList.RemoveAt(i);
                m_dicQuery.Remove(p.LogicID);
                ReferencePool.Release(p);
            }
        }
    }

    public void ClearAll()
    {
        for (int i = m_updateList.Count - 1; i >= 0; --i)
        {
            FlyPlayer p = m_updateList[i];
            m_updateList.RemoveAt(i);
            m_dicQuery.Remove(p.LogicID);
            ReferencePool.Release(p);
        }
        m_IdAllocer = 1;
    }

    private void LateUpdate()
    {
        OnLateUpdate( Time.deltaTime );
    }
}
 
